<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.blurry" clearable size="small" placeholder="姓名、身份证" style="width: 170px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="住院号" prop="bisJzdjLsh">
            <el-input v-model="form.bisJzdjLsh" style="width: 270px;" readonly />
          </el-form-item>
          <el-form-item label="住院科室" prop="bisJzdjKsmc">
            <el-select
              v-model="form.bisJzdjKsmc"
              style="width: 270px"
              placeholder="选择住院科室"
            >
              <el-option
                v-for="item in allHljgKs"
                :key="item.bisHljgKsName"
                :label="item.bisHljgKsName"
                :value="item.bisHljgKsName"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="病室" prop="bisJzdjBs">
            <el-input v-model="form.bisJzdjBs" style="width: 270px;" />
          </el-form-item>
          <el-form-item label="床位号" prop="bisJzdjCwh">
            <el-input v-model="form.bisJzdjCwh" style="width: 270px;" />
          </el-form-item>
          <el-form-item label="医生姓名" prop="bisJzdjYsxm">
            <el-input v-model="form.bisJzdjYsxm" style="width: 270px;" />
          </el-form-item>
          <el-form-item label="患者联系方式" prop="bisJzdjHzlxfs">
            <el-input v-model="form.bisJzdjHzlxfs" style="width: 270px;" />
          </el-form-item>
          <el-form-item label="家属联系方式">
            <el-input v-model="form.bisJzdjJslxfs" style="width: 270px;" />
          </el-form-item>
          <el-form-item label="入院日期" prop="bisJzdjRyrq">
            <el-date-picker v-model="form.bisJzdjRyrq" type="date" value-format="yyyy-MM-dd" style="width: 270px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <el-dialog :visible.sync="dialogWfcyFormVisible" title="无费出院" width="500px">
        <el-form>
          <el-form-item label="姓名" prop="xm">
            <el-input v-model="xm" style="width: 150px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogWfcyFormVisible = false">取消</el-button>
          <el-button v-loading.fullscreen.lock="fullscreenLoading" type="primary" @click="doWfcy()">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column align="center" prop="bisZhsqSnryXm" label="患者姓名" width="90" />
        <el-table-column align="center" prop="bisZhsqSnrySfz" label="身份证" />
        <el-table-column align="center" prop="bisJzdjHzlxfs" label="患者联系方式" width="95" />
        <el-table-column align="center" prop="bisJzdjJslxfs" label="家属联系方式" width="95" />
        <el-table-column align="center" prop="bisZhsqDykkrq" label="待遇开始日期" width="100" />
        <el-table-column align="center" prop="bisZhsqDyjsrq" label="待遇结束日期" width="100" />
        <el-table-column align="center" prop="bisZhsqSndj" label="失能等级" width="90" />
        <el-table-column align="center" prop="bisJzdjRyrq" label="入院日期" width="90" />
        <el-table-column align="center" prop="lrsj" label="经办日期" />
        <el-table-column label="操作" fixed="right" width="150px" align="center">
          <template slot-scope="scope">
            <el-dropdown show-timeout="50" hide-timeout="150" size="medium" @command="handleCommand">
              <el-button type="primary">
                更多菜单<i class="el-icon-arrow-down el-icon--right" />
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <!--
                <el-dropdown-item
                  icon="el-icon-edit"
                  :command="beforeHandleCommand('edit', scope.row)">
                  登记信息修改
                </el-dropdown-item>
                -->
                <el-dropdown-item
                  icon="el-icon-setting"
                  :command="beforeHandleCommand('fee', scope.row)"
                >
                  固费设置
                </el-dropdown-item>
                <el-dropdown-item
                  icon="el-icon-document"
                  :command="beforeHandleCommand('bill', scope.row)"
                >
                  住院记账
                </el-dropdown-item>
                <el-dropdown-item
                  icon="el-icon-menu"
                  :command="beforeHandleCommand('settle', scope.row)"
                >
                  出院结算
                </el-dropdown-item>
                <el-dropdown-item
                  icon="el-icon-rank"
                  :command="beforeHandleCommand('nocharge', scope.row)"
                >
                  无费出院
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
      <!-- 固费设置页面 -->
      <fixedCostsIndex ref="fixedCostsIndex" />
      <!-- 住院记账页面 -->
      <accountingCalendar ref="accountingCalendar" />
      <!-- 费用结算 -->
      <jyjs ref="jyjs" />
    </div>
  </div>
</template>

<script>
import jyjs from './jyjs'
import fixedCostsIndex from '../fixedCosts/index'
import accountingCalendar from '../jzhang/accountingCalendar'
import crudJzdj from '@/api/business/jzdj'
import { queryAllHljgKs } from '@/api/business/hljgKs'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import moment from 'moment'

const defaultForm = { bisJzdjId: null, bisJzdjZhsqId: null, bisJzdjLsh: null, bisJzdjYllb: null, bisJzdjRyrq: null, bisJzdjCyrq: null, bisJzdjRyjbbm: null, bisJzdjGhf: null, bisJzdjZcf: null, bisJzdjCyjbbm: null, bisJzdjJbr: null, bisJzdjJbrq: null, bisJzdjFph: null, bisJzdjCyzt: null, bisJzdjSpr: null, bisJzdjZcyymc: null, bisJzdjKsmc: null, bisJzdjBs: null, bisJzdjCwh: null, bisJzdjYsxm: null, bisJzdjHzlxfs: null, bisJzdjJslxfs: null, bisJzdjSpkssj: null, bisJzdjSpzzsj: null, ywlx: null, lrr: null, lrrxm: null, lrbm: null, lrbmmc: null, lrsj: null, gxr: null, gxrxm: null, gxbm: null, gxbmmc: null, gxsj: null, sfyx: null, bz: null }
export default {
  name: 'Jzdj',
  components: { pagination, crudOperation, rrOperation, udOperation, fixedCostsIndex, accountingCalendar, jyjs },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['xb', 'yjsgx', 'ccs', 'zhzt', 'ccstcqh'],
  cruds() {
    return CRUD({ title: '在院人员', url: 'api/jzdj/queryZyry', idField: 'bisJzdjId', sort: 'lrsj,desc', crudMethod: { ...crudJzdj }})
  },
  data() {
    return {
      allHljgKs: [],
      dialogWfcyFormVisible: false,
      fullscreenLoading: false,
      jzid: '',
      cyrq: '',
      xm: '',
      permission: {
        add: ['admin', 'jzdj:add'],
        edit: ['admin', 'jzdj:edit'],
        del: ['admin', 'jzdj:del']
      },
      rules: {
        bisJzdjRyrq: [
          { required: true, message: '入院日期不能为空', trigger: 'blur' }
        ],
        bisJzdjKsmc: [
          { required: true, message: '科室名称不能为空', trigger: 'blur' }
        ],
        bisJzdjBs: [
          { required: true, message: '病室不能为空', trigger: 'blur' }
        ],
        bisJzdjCwh: [
          { required: true, message: '床位号不能为空', trigger: 'blur' }
        ],
        bisJzdjYsxm: [
          { required: true, message: '医生姓名不能为空', trigger: 'blur' }
        ],
        bisJzdjHzlxfs: [
          { required: true, message: '患者联系方式不能为空', trigger: 'blur' }
        ],
        bisJzdjSpkssj: [
          { required: true, message: '审批开始时间不能为空', trigger: 'blur' }
        ],
        bisJzdjSpzzsj: [
          { required: true, message: '审批终止时间不能为空', trigger: 'blur' }
        ]
      }}
  },
  created() {
    this.crud.updateProp('searchToggle', true)
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 初始化编辑时候的角色与岗位
    [CRUD.HOOK.beforeToEdit](crud, form) {
      this.queryAllHljgKs()
      console.log(this.form.bisJzdjRyrq)
      console.log(moment(this.form.bisJzdjRyrq).toDate())
      this.form.bisJzdjRyrq = moment(this.form.bisJzdjRyrq).toDate() // moment(e.day).format('YYYYMMDD')
    },
    // 提交前做的操作
    [CRUD.HOOK.afterValidateCU](crud) {
      return true
    },
    beforeHandleCommand(type, row) {
      return {
        'type': type,
        'row': row
      }
    },
    handleCommand(res) {
      const type = res.type
      const row = res.row
      switch (type) {
        // 登记信息修改
        case 'edit':
          this.crud.toEdit(row)
          break
        // 固费设置
        case 'fee':
          this.$refs.fixedCostsIndex.fixedCostsDialog = true
          this.$refs.fixedCostsIndex.zhid = row.bisZhsqId
          this.$refs.fixedCostsIndex.jzid = row.bisJzdjId
          this.$refs.fixedCostsIndex.zxks = row.bisJzdjKsmc
          this.$refs.fixedCostsIndex.query.bisFixedCostsJzid = row.bisJzdjId
          this.$refs.fixedCostsIndex.query.bisFixedCostsZhid = row.bisZhsqId
          this.$refs.fixedCostsIndex.crud.toQuery()
          break
        // 住院记账
        case 'bill':
          var date = moment(new Date()).format('YYYYMMDD')
          this.$refs.accountingCalendar.accountingCalendarDialog = true
          this.$refs.accountingCalendar.jzid = row.bisJzdjId
          this.$refs.accountingCalendar.zxks = row.bisJzdjKsmc
          this.$refs.accountingCalendar.jzlsh = row.bisJzdjLsh
          this.$refs.accountingCalendar.ysxm = row.bisJzdjYsxm
          this.$refs.accountingCalendar.grbh = row.bisZhsqGrbh
          this.$refs.accountingCalendar.ybkh = row.bisZhsqYbkh
          this.$refs.accountingCalendar.ryrq = row.bisJzdjRyrq
          this.$refs.accountingCalendar.query.bisFymxJzdjId = row.bisJzdjId
          this.$refs.accountingCalendar.query.bisFymxKfrq = date
          this.$refs.accountingCalendar.crud.toQuery()

          this.$refs.accountingCalendar.test()
          this.$refs.accountingCalendar.groupByKfrq2Zj()
          break
        // 住院记账
        case 'settle':
          this.$refs.jyjs.js_ylzfy = ''
          this.$refs.jyjs.js_tczf = ''
          this.$refs.jyjs.js_xjzf = ''
          this.$refs.jyjs.js_fwjgbh = ''
          this.$refs.jyjs.js_jzlsh = ''
          this.$refs.jyjs.js_fph = ''
          this.$refs.jyjs.js_tczfbl = ''
          this.$refs.jyjs.fyjsDialog = true
          this.$refs.jyjs.xm = row.bisZhsqSnryXm
          this.$refs.jyjs.sfz = row.bisZhsqSnrySfz
          this.$refs.jyjs.jzid = row.bisJzdjId
          this.$refs.jyjs.zhid = row.bisZhsqId
          this.$refs.jyjs.query.bisFymxJzdjId = row.bisJzdjId
          this.$refs.jyjs.crud.toQuery()
          this.$refs.jyjs.queryLocalFyxx()
          break
        case 'nocharge':
          this.doOpenWfcy(row)
          break
      }
    },
    // 获取所有机构科室
    queryAllHljgKs() {
      queryAllHljgKs().then(res => {
        this.allHljgKs = res
      }).catch(() => { })
    },
    doOpenWfcy(row) {
      this.xm = row.bisZhsqSnryXm
      this.jzid = row.bisJzdjId
      this.cyrq = row.bisJzdjRyrq
      this.dialogWfcyFormVisible = true
    },
    doWfcy() {
      if (this.cyrq === undefined || this.cyrq === '') {
        this.$notify({
          title: '请填写出院日期',
          type: 'warning',
          duration: 2500
        })
        return
      }
      this.$confirm('你确定无费出院吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.fullscreenLoading = true
        crudJzdj.exeWfcy({ jzid: this.jzid, cyrq: this.cyrq })
          .then(res => {
            this.fullscreenLoading = false
            this.dialogWfcyFormVisible = false
            this.$notify({
              title: '已出院',
              type: 'success',
              duration: 2500
            })
            this.crud.toQuery()
          })
          .catch(err => {
            this.fullscreenLoading = false
            this.dialogWfcyFormVisible = false
            console.log(err)
          })
      })
    }
  }
}
</script>

<style scoped>

</style>
